<?php

namespace App\Models;

use Cache;

class WaterBillCategory extends BaseModel
{
    protected $table = "waterbill_category";

    const WATERBILL_DATA = 'WATERBILL_DATA';
    const WATERBILL_ALL_DATA = 'WATERBILL_ALL_DATA';

    protected $rules = [
        'title' => 'required|between:1,18',
        'buy_num' => 'required|numeric|min:1',
        'gift_num' => 'required|numeric',
    ];

    protected $message = [
        'title.required' => '套餐名称必须填写',
        'title.between' => '套餐名称不合法必须1-18个字',
        'buy_num.required' => '购买数量必须填写',
        'buy_num.numeric' => '购买数量必须是纯数字',
        'buy_num.min' => '购买数量不能小于1',
        'gift_num.required' => '赠送数量必须填写',
        'gift_num.numeric' => '赠送数量必须是纯数字',
        'gift_num.min' => '赠送数量不能小于1',
    ];

    public function addOrUpdate($type = 0)
    {
        $data['admin_id'] = plat_admin_id();
        $data['title'] = trim(request('title'));
        $data['buy_num'] = intval(request('buy_num', 0));
        $data['gift_num'] = intval(request('gift_num', 0));
        $data['total_num'] = $data['buy_num'] + $data['gift_num'];
        $data['description'] = trim(request('description'));
        $data['status'] = trim(request('status', 1));
        $data['sort'] = trim(request('sort', 0));
        if (request('id')) {
            $data['id'] = trim(request('id'));
        } 
        if (!$this->_validate($data)) {
            return false;
        }
        $this->_start();
        try {
            $data['id'] = $this->_save($data);
            if ($type) {
                $shopWaterBillCategory = new ShopWaterBillCategory();
                $category = $shopWaterBillCategory->_getOne(['shop_id' => session('shop_info')['id']]);
                $sdata['admin_id'] = plat_admin_id();
                $sdata['shop_id'] = session('shop_info')['id'];
                $arr = $data;
                if (!$category) {
                    $sdata['category_data'][0] = $arr;
                } else {
                    $sdata['id'] = $category['id'];
                    $sdata['category_data'] = $category['category_data'];
                    if (request('id')) {
                        foreach ($sdata['category_data'] as &$v) {
                            if ($v['id'] == $data['id']) {
                                $v = $data;
                            }
                        }
                    } else {
                        $index = count($category['category_data']);
                        $sdata['category_data'][$index] = $arr;
                    }

                }
                $shopWaterBillCategory->_save($sdata);
            }
            $this->_commit();
            return true;
        } catch (\Exception $e) {
            $this->_rollBack();
            return false;
        }
    }

    public function del()
    {
        $id = request('id');
        if (!$id) {
            $this->error = '请选择要操作的数据!';
            return false;
        }
        $child = $this->_getList(['pid' => $id])->toArray();
        if (!empty($child)) {
            $this->error = '请先删除该品牌下的子品牌';
            return false;
        }
        $res = $this->_del(['id' => $id]);
        if (!$res) {
            $this->error = '删除品牌失败!';
            return false;
        }
        return '删除品牌成功';
    }

}
